package majorityElement;

import java.util.HashMap;
import java.util.Map;

/**
 * @author hht
 * @ClassName Solution
 * @Description TODO
 * @Date 2019/6/5 15:10
 * @VERSION 1.0
 */
public class Solution {

    public static void main(String[] args) {
        int []nums = {2,2,1,1,1,2,2};
        System.out.println(majorityElement(nums));
    }
    public static int majorityElement(int[] nums) {
        int result = nums[0];
        int count = 0;
        Map<Integer,Integer> countMap = new HashMap<>();
        for(int i=0;i<nums.length;i++){
            if(countMap.get(nums[i])==null){
                countMap.put(nums[i],1);
            }else{
                int newCount = countMap.get(nums[i])+1;
                countMap.put(nums[i],newCount);
                if(newCount>count){
                    result = nums[i];
                    count = newCount;
                }
            }
        }
        return result;
    }

}
